<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=US-ASCII">
<title>PPK file format</title>
<link rel="previous" href="AppendixB.html">
<link rel="ToC" href="index.html">
<link rel="up" href="index.html">
<link rel="index" href="IndexPage.html">
<link rel="next" href="AppendixD.html">
</head>
<body>
<p><a href="AppendixB.html">Previous</a> | <a href="index.html">Contents</a> | <a href="IndexPage.html">Index</a> | <a href="AppendixD.html">Next</a></p>

<ul>
<li><a href="#ppk">Appendix C: PPK file format</a>
<ul>
<li><a href="#ppk-overview">C.1 Overview</a></li>
<li><a href="#ppk-outer">C.2 Outer layer</a></li>
<li><a href="#ppk-privkeys">C.3 Private key encodings</a>
<ul>
<li><a href="#ppk-privkey-rsa">C.3.1 RSA</a></li>
<li><a href="#ppk-privkey-dsa">C.3.2 DSA</a></li>
<li><a href="#ppk-privkey-ecdsa">C.3.3 NIST elliptic-curve keys</a></li>
<li><a href="#ppk-privkey-eddsa">C.3.4 EdDSA elliptic-curve keys (Ed25519 and Ed448)</a></li>
</ul></li>
<li><a href="#ppk-keys">C.4 Key derivation</a></li>
<li><a href="#ppk-old">C.5 Older versions of the PPK format</a>
<ul>
<li><a href="#ppk-v2">C.5.1 Version 2</a></li>
<li><a href="#ppk-v1">C.5.2 Version 1</a></li>
</ul></li>
</ul></li>
</ul>
<h1><a name="ppk"></a><a name="AC"></a>Appendix C: PPK file format</h1>
<p>
This appendix documents the file format used by PuTTY to store private keys.
</p>
<p>
In this appendix, binary data structures are described using data type representations such as &#8216;<code>uint32</code>&#8217;, &#8216;<code>string</code>&#8217; and &#8216;<code>mpint</code>&#8217; as used in the SSH protocol standards themselves. These are defined authoritatively by <a href="https://www.rfc-editor.org/rfc/rfc4251#section-5">RFC 4251 section 5</a>, &#8216;Data Type Representations Used in the SSH Protocols&#8217;.
</p>
<h2><a name="ppk-overview"></a><a name="SC.1"></a>C.1 Overview</h2>
<p>
A PPK file stores a private key, and the corresponding public key. Both are contained in the same file.
</p>
<p>
The file format can be completely unencrypted, or it can encrypt the private key. The <em>public</em> key is stored in cleartext in both cases. (This enables PuTTY to send the public key to an SSH server to see whether it will accept it, and not bother prompting for the passphrase unless the server says yes.)
</p>
<p>
When the key file is encrypted, the encryption key is derived from a passphrase. An encrypted PPK file is also tamper-proofed using a MAC (authentication code), also derived from the same passphrase. The MAC protects the encrypted private key data, but it also covers the cleartext parts of the file. So you can't edit the public half of the key without invalidating the MAC and causing the key file as a whole to become useless.
</p>
<p>
This MAC protects the key file against active cryptographic attacks in which the public half of a key pair is modified in a controlled way that allows an attacker to deduce information about the private half from the resulting corrupted signatures. Any attempt to do that to a PPK file should be reliably caught by the MAC failing to validate.
</p>
<p>
(Such an attack would only be useful if the key file was stored in a location where the attacker could modify it without also having full access to the process that you type passphrases into. But that's not impossible; for example, if your home directory was on a network file server, then the file server's administrator could access the key file but not processes on the client machine.)
</p>
<p>
The MAC also covers the <em>comment</em> on the key. This stops an attacker from swapping keys with each other and editing the comments to disguise the fact. As a consequence, PuTTYgen cannot edit the comment on a key unless you decrypt the key with your passphrase first.
</p>
<p>
(The circumstances in which <em>that</em> attack would be useful are even more restricted. One example might be that the different keys trigger specific actions on the server you're connecting to and one of those actions is more useful to the attacker than the other. But once you have a MAC at all, it's no extra effort to make it cover as much as possible, and usually sensible.)
</p>
<h2><a name="ppk-outer"></a><a name="SC.2"></a>C.2 Outer layer</h2>
<p>
The outer layer of a PPK file is text-based. The PuTTY tools will always use LF line termination when writing PPK files, but will tolerate CR+LF and CR-only on input.
</p>
<p>
The first few lines identify it as a PPK, and give some initial data about what's stored in it and how. They look like this:
</p>
<pre><code>PuTTY-User-Key-File-<b>version</b>: <b>algorithm-name</b>
Encryption: <b>encryption-type</b>
Comment: <b>key-comment-string</b>
</code></pre>
<p>
<strong>version</strong> is a decimal number giving the version number of the file format itself. The current file format version is 3.
</p>
<p>
<strong>algorithm-name</strong> is the SSH protocol identifier for the public key algorithm that this key is used for (such as &#8216;<code>ssh-dss</code>&#8217; or &#8216;<code>ecdsa-sha2-nistp384</code>&#8217;).
</p>
<p>
<strong>encryption-type</strong> indicates whether this key is stored encrypted, and if so, by what method. Currently the only supported encryption types are &#8216;<code>aes256-cbc</code>&#8217; and &#8216;<code>none</code>&#8217;.
</p>
<p>
<strong>key-comment-string</strong> is a free text field giving the comment. This can contain any byte values other than 13 and 10 (CR and LF).
</p>
<p>
The next part of the file gives the public key. This is stored unencrypted but base64-encoded (<a href="https://www.rfc-editor.org/rfc/rfc4648">RFC 4648</a>), and is preceded by a header line saying how many lines of base64 data are shown, looking like this:
</p>
<pre><code>Public-Lines: <b>number-of-lines</b>
<b>that many lines of base64 data</b>
</code></pre>
<p>
The base64-encoded data in this blob is formatted in exactly the same way as an SSH public key sent over the wire in the SSH protocol itself. That is also the same format as the base64 data stored in OpenSSH's <code>authorized_keys</code> file, except that in a PPK file the base64 data is split across multiple lines. But if you remove the newlines from the middle of this section, the resulting base64 blob is in the right format to go in an <code>authorized_keys</code> line.
</p>
<p>
If the key is encrypted (i.e. <strong>encryption-type</strong> is not &#8216;<code>none</code>&#8217;), then the next thing that appears is a sequence of lines specifying how the keys for encrypting the file are to be derived from the passphrase:
</p>
<pre><code>Key-Derivation: <b>argon2-flavour</b>
Argon2-Memory: <b>decimal-integer</b>
Argon2-Passes: <b>decimal-integer</b>
Argon2-Parallelism: <b>decimal-integer</b>
Argon2-Salt: <b>hex-string</b>
</code></pre>
<p>
<strong>argon2-flavour</strong> is one of the identifiers &#8216;<code>Argon2d</code>&#8217;, &#8216;<code>Argon2i</code>&#8217; or &#8216;<code>Argon2id</code>&#8217;, all describing variants of the Argon2 password-hashing function.
</p>
<p>
The three integer values are used as parameters for Argon2, which allows you to configure the amount of memory used (in Kbyte), the number of passes of the algorithm to run (to tune its running time), and the degree of parallelism required by the hash function. The salt is decoded into a sequence of binary bytes and used as an additional input to Argon2. (It is chosen randomly when the key file is written, so that a guessing attack can't be mounted in parallel against multiple key files.)
</p>
<p>
The next part of the file gives the private key. This is base64-encoded in the same way:
</p>
<pre><code>Private-Lines: <b>number-of-lines</b>
<b>that many lines of base64 data</b>
</code></pre>
<p>
The binary data represented in this base64 blob may be encrypted, depending on the <em>encryption-type</em> field in the key file header shown above:
</p>
<ul><li>
If <strong>encryption-type</strong> is &#8216;<code>none</code>&#8217;, then this data is stored in plain text.
</li>
<li>
If <strong>encryption-type</strong> is &#8216;<code>aes256-cbc</code>&#8217;, then this data is encrypted using AES, with a 256-bit key length, in the CBC cipher mode. The key and initialisation vector are derived from the passphrase: see <a href="#ppk-keys">section C.4</a>.
<p>
In order to encrypt the private key data with AES, it must be a multiple of 16 bytes (the AES cipher block length). This is achieved by appending random padding to the data before encrypting it. When decoding it after decryption, the random data can be ignored: the internal structure of the data is enough to tell you when you've reached the end of the meaningful part.
</p>

</li>
</ul>
<p>
Unlike public keys, the binary encoding of private keys is not specified at all in the SSH standards. See <a href="#ppk-privkeys">section C.3</a> for details of the private key format for each key type supported by PuTTY.
</p>
<p>
The final thing in the key file is the MAC:
</p>
<pre><code>Private-MAC: <b>hex-mac-data</b>
</code></pre>
<p>
<strong>hex-mac-data</strong> is a hexadecimal-encoded value, 64 digits long (i.e. 32 bytes), generated using the HMAC-SHA-256 algorithm with the following binary data as input:
</p>
<ul><li>
<code>string</code>: the <strong>algorithm-name</strong> header field.
</li>
<li>
<code>string</code>: the <strong>encryption-type</strong> header field.
</li>
<li>
<code>string</code>: the <strong>key-comment-string</strong> header field.
</li>
<li>
<code>string</code>: the binary public key data, as decoded from the base64 lines after the &#8216;<code>Public-Lines</code>&#8217; header.
</li>
<li>
<code>string</code>: the plaintext of the binary private key data, as decoded from the base64 lines after the &#8216;<code>Private-Lines</code>&#8217; header. If that data was stored encrypted, then the decrypted version of it is used in this MAC preimage, <em>including</em> the random padding mentioned above.
</li>
</ul>
<p>
The MAC key is derived from the passphrase: see <a href="#ppk-keys">section C.4</a>.
</p>
<h2><a name="ppk-privkeys"></a><a name="SC.3"></a>C.3 Private key encodings</h2>
<p>
This section describes the private key format for each key type supported by PuTTY.
</p>
<p>
Because the PPK format also contains the public key (and both public and private key are protected by the same MAC to ensure they can't be made inconsistent), there is no need for the private key section of the file to repeat data from the public section. So some of these formats are very short.
</p>
<p>
In all cases, a decoding application can begin reading from the start of the decrypted private key data, and know when it has read all that it needs. This allows random padding after the meaningful data to be safely ignored.
</p>
<h3><a name="ppk-privkey-rsa"></a><a name="SC.3.1"></a>C.3.1 RSA</h3>
<p>
RSA keys are stored using an <strong>algorithm-name</strong> of &#8216;<code>ssh-rsa</code>&#8217;. (Keys stored like this are also used by the updated RSA signature schemes that use hashes other than SHA-1.)
</p>
<p>
The public key data has already provided the key modulus and the public encoding exponent. The private data stores:
</p>
<ul><li>
<code>mpint</code>: the private decoding exponent of the key.
</li>
<li>
<code>mpint</code>: one prime factor <em>p</em> of the key.
</li>
<li>
<code>mpint</code>: the other prime factor <em>q</em> of the key. (RSA keys stored in this format are expected to have exactly two prime factors.)
</li>
<li>
<code>mpint</code>: the multiplicative inverse of <em>q</em> modulo <em>p</em>.
</li>
</ul>
<h3><a name="ppk-privkey-dsa"></a><a name="SC.3.2"></a>C.3.2 DSA</h3>
<p>
DSA keys are stored using an <strong>algorithm-name</strong> of &#8216;<code>ssh-dss</code>&#8217;.
</p>
<p>
The public key data has already provided the key parameters (the large prime <em>p</em>, the small prime <em>q</em> and the group generator <em>g</em>), and the public key <em>y</em>. The private key stores:
</p>
<ul><li>
<code>mpint</code>: the private key <em>x</em>, which is the discrete logarithm of <em>y</em> in the group generated by <em>g</em> mod <em>p</em>.
</li>
</ul>
<h3><a name="ppk-privkey-ecdsa"></a><a name="SC.3.3"></a>C.3.3 NIST elliptic-curve keys</h3>
<p>
<a name="i0"></a>NIST elliptic-curve keys are stored using one of the following <strong>algorithm-name</strong> values, each corresponding to a different elliptic curve and key size:
</p>
<ul><li>
&#8216;<code>ecdsa-sha2-nistp256</code>&#8217;
</li>
<li>
&#8216;<code>ecdsa-sha2-nistp384</code>&#8217;
</li>
<li>
&#8216;<code>ecdsa-sha2-nistp521</code>&#8217;
</li>
</ul>
<p>
The public key data has already provided the public elliptic curve point. The private key stores:
</p>
<ul><li>
<code>mpint</code>: the private exponent, which is the discrete log of the public point.
</li>
</ul>
<h3><a name="ppk-privkey-eddsa"></a><a name="SC.3.4"></a>C.3.4 EdDSA elliptic-curve keys (Ed25519 and Ed448)</h3>
<p>
EdDSA elliptic-curve keys are stored using one of the following <strong>algorithm-name</strong> values, each corresponding to a different elliptic curve and key size:
</p>
<ul><li>
&#8216;<code>ssh-ed25519</code>&#8217;
</li>
<li>
&#8216;<code>ssh-ed448</code>&#8217;
</li>
</ul>
<p>
The public key data has already provided the public elliptic curve point. The private key stores:
</p>
<ul><li>
<code>mpint</code>: the private exponent, which is the discrete log of the public point.
</li>
</ul>
<h2><a name="ppk-keys"></a><a name="SC.4"></a>C.4 Key derivation</h2>
<p>
When a key file is encrypted, there are three pieces of key material that need to be computed from the passphrase:
</p>
<ul><li>
the key for the symmetric cipher used to encrypt the private key
</li>
<li>
the initialisation vector for that cipher encryption
</li>
<li>
the key for the MAC.
</li>
</ul>
<p>
If <strong>encryption-type</strong> is &#8216;<code>aes256-cbc</code>&#8217;, then the symmetric cipher key is 32 bytes long, and the initialisation vector is 16 bytes (one cipher block). The length of the MAC key is also chosen to be 32 bytes.
</p>
<p>
If <strong>encryption-type</strong> is &#8216;<code>none</code>&#8217;, then all three of these pieces of data have zero length. (The MAC is still generated and checked in the key file format, but it has a zero-length key.)
</p>
<p>
If the amount of key material required is not zero, then the passphrase is fed to the Argon2 key derivation function, in whichever mode is described in the &#8216;<code>Key-Derivation</code>&#8217; header in the key file, with parameters derived from the various &#8216;<code>Argon2-</code><em>Parameter</em><code>:</code>&#8217; headers.
</p>
<p>
(If the key is unencrypted, then all those headers are omitted, and Argon2 is not run at all.)
</p>
<p>
Argon2 takes two extra string inputs in addition to the passphrase and the salt: a secret key, and some &#8216;associated data&#8217;. In PPK's use of Argon2, these are both set to the empty string.
</p>
<p>
The &#8216;tag length&#8217; parameter to Argon2 (i.e. the amount of data it is asked to output) is set to the sum of the lengths of all of the data items required, i.e. (cipher key length + IV length + MAC key length). The output data is interpreted as the concatenation of the cipher key, the IV and the MAC key, in that order.
</p>
<p>
So, for &#8216;<code>aes256-cbc</code>&#8217;, the tag length will be 32+16+32&nbsp;=&nbsp;80 bytes; of the 80 bytes of output data, the first 32 bytes are used as the 256-bit AES key, the next 16 as the CBC IV, and the final 32 bytes as the HMAC-SHA-256 key.
</p>
<h2><a name="ppk-old"></a><a name="SC.5"></a>C.5 Older versions of the PPK format</h2>
<h3><a name="ppk-v2"></a><a name="SC.5.1"></a>C.5.1 Version 2</h3>
<p>
PPK version 2 was used by PuTTY 0.52 to 0.74 inclusive.
</p>
<p>
In PPK version 2, the MAC algorithm used was HMAC-SHA-1 (so the <code>Private-MAC</code> line contained only 40 hex digits).
</p>
<p>
The &#8216;<code>Key-Derivation:</code>&#8217; header and all the &#8216;<code>Argon2-</code><em>Parameter</em><code>:</code>&#8217; headers were absent. Instead of using Argon2, the key material for encrypting the private blob was derived from the passphrase in a totally different way, as follows.
</p>
<p>
The cipher key for &#8216;<code>aes256-cbc</code>&#8217; was constructed by generating two SHA-1 hashes, concatenating them, and taking the first 32 bytes of the result. (So you'd get all 20 bytes of the first hash output, and the first 12 of the second). Each hash preimage was as follows:
</p>
<ul><li>
<code>uint32</code>: a sequence number. This is 0 in the first hash, and 1 in the second. (The idea was to extend this mechanism to further hashes by continuing to increment the sequence number, if future changes required even longer keys.)
</li>
<li>
the passphrase, without any prefix length field.
</li>
</ul>
<p>
In PPK v2, the CBC initialisation vector was all zeroes.
</p>
<p>
The MAC key was 20 bytes long, and was a single SHA-1 hash of the following data:
</p>
<ul><li>
the fixed string &#8216;<code>putty-private-key-file-mac-key</code>&#8217;, without any prefix length field.
</li>
<li>
the passphrase, without any prefix length field. (If the key is stored unencrypted, the passphrase was taken to be the empty string for these purposes.)
</li>
</ul>
<h3><a name="ppk-v1"></a><a name="SC.5.2"></a>C.5.2 Version 1</h3>
<p>
PPK version 1 was a badly designed format, only used during initial development, and not recommended for production use.
</p>
<p>
PPK version 1 was never used by a released version of PuTTY. It was only emitted by some early development snapshots between version 0.51 (which did not support SSH-2 public keys at all) and 0.52 (which already used version 2 of this file format). I <em>hope</em> there are no PPK v1 files in use anywhere. But just in case, the old badly designed format is documented here anyway.
</p>
<p>
In PPK version 1, the input to the MAC does not include any of the header fields or the public key. It is simply the private key data (still in plaintext and including random padding), all by itself (without a wrapping <code>string</code>).
</p>
<p>
PPK version 1 keys must therefore be rigorously validated after loading, to ensure that the public and private parts of the key were consistent with each other.
</p>
<p>
PPK version 1 only supported the RSA and DSA key types. For RSA, this validation can be done using only the provided data (since the private key blob contains enough information to reconstruct the public values anyway). But for DSA, that isn't quite enough.
</p>
<p>
Hence, PPK version 1 DSA keys extended the private data so that immediately after <em>x</em> was stored an extra value:
</p>
<ul><li>
<code>string</code>: a SHA-1 hash of the public key data, whose preimage consists of
<ul><li>
<code>string</code>: the large prime <em>p</em>
</li>
<li>
<code>string</code>: the small prime <em>q</em>
</li>
<li>
<code>string</code>: the group generator <em>g</em>
</li>
</ul>

</li>
</ul>
<p>
The idea was that checking this hash would verify that the key parameters had not been tampered with, and then the loading application could directly verify that <em>g</em><code>^</code><em>x</em><code>&nbsp;=&nbsp;</code><em>y</em>.
</p>
<p>
In an <em>unencrypted</em> version 1 key file, the MAC is replaced by a plain SHA-1 hash of the private key data. This is indicated by the &#8216;<code>Private-MAC:</code>&#8217; header being replaced with &#8216;<code>Private-Hash:</code>&#8217; instead.
</p>

<hr><p>If you want to provide feedback on this manual or on the PuTTY tools themselves, see the <a href="https://www.chiark.greenend.org.uk/~sgtatham/putty/feedback.html">Feedback page</a>.</p><address>
[PuTTY release 0.81]</address></body>
</html>
